package web.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.util.Map;

@WebServlet("/d2")
public class ServletDemo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("get.....");
        Map<String,String[]> map= req.getParameterMap();
        for(String key : map.keySet())
        {
            //所有键
            System.out.print(key+" : ");

            //获取值的数组
            String[] values = map.get(key);
            for(String value:values)
            {
                System.out.print(value+" ");
            }
            System.out.println();
        }

        System.out.println("-------------");
        //根据key获取参数值数组
        String[] hobbies = req.getParameterValues("hobby");     //这里不能为空
        for(String h : hobbies)
        {
            System.out.println(h);
        }

        System.out.println("---------------");
        //根据key获取单个参数值
        String username = req.getParameter("username");
        String password = req.getParameter("password");
//        username = new String(username.getBytes(StandardCharsets.ISO_8859_1),"UTF-8");  //tomcat7中文乱码解决方案
        System.out.println(username+password);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("post...");
        req.setCharacterEncoding("UTF-8");        //tomcat的底层流是ISO的，需要修改为gbk或utf-8，设置字符输入流的编码

        this.doGet(req, resp);
    }
}
